<template>
  <t-card class="article-card" @click="handleClick">
    <div class="article-header">
      <img v-if="article.user_avatar" :src="article.user_avatar" class="user-avatar" />
    </div>
    <div class="article-content">
      <h4 class="article-title">{{ article.title }}</h4>
      <div class="article-summary">{{ article.content_text }}</div>
    </div>
    <div class="article-footer">
      <div class="article-meta">
        <span class="article-author">{{ article.user_nickname }}</span>
        <span class="article-time">{{ formattedCreatedTime }}</span>
      </div>
      <button class="article-details-btn" @click.stop="handleClick">查看详情</button>
    </div>
  </t-card>
</template>

<script>
export default {
  name: 'ArticleCard',
  props: {
    article: {
      type: Object,
      required: true,
    },
  },
  computed: {
    formattedCreatedTime() {
      return new Date(this.article.created_time).toLocaleString();
    },
  },
  methods: {
    handleClick() {
      this.$emit('showDetails', this.article);
    },
  },
};
</script>

<style scoped>
.article-card {
  cursor: pointer;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: box-shadow 0.3s ease;
  border-radius: 8px; /* 增加圆角 */
  box-sizing: border-box; /* 确保内边距不影响宽度 */
}

.article-card:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}

.article-header {
  width: 100%;
  text-align: center;
  margin-bottom: 16px;
}

.user-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

.article-content {
  flex-grow: 1;
  text-align: center; /* 文字居中 */
  margin-bottom: 16px;
  overflow: hidden;
}

.article-title {
  margin: 0;
  font-size: 18px;
  color: #333;
}

.article-summary {
  margin: 10px 0;
  font-size: 14px;
  color: #666;
  height: 3em; /* 固定高度以控制内容溢出 */
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2; /* 限制两行文本 */
  -webkit-box-orient: vertical;
}

.article-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.article-meta {
  font-size: 12px;
  color: #999;
}

.article-author::after {
  content: ' •';
  margin: 0 5px;
}

.article-time {
  margin-left: 8px; /* 时间与作者之间的间距 */
}

.article-details-btn {
  padding: 6px 12px;
  background-color: #007bff; /* 按钮背景颜色 */
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.article-details-btn:hover {
  background-color: #0056b3; /* 按钮悬停背景颜色 */
}

/* 响应式调整 */
@media (max-width: 768px) {
  .user-avatar {
    width: 60px;
    height: 60px;
  }

  .article-title {
    font-size: 16px;
  }

  .article-summary {
    font-size: 13px;
    height: 2.6em;
  }

  .article-details-btn {
    padding: 5px 10px;
    font-size: 12px;
  }
}
</style>
